import { NextRequest } from 'next/server'
import { userQueries } from './database-prisma'

export async function getUserFromRequest(request: NextRequest) {
  try {
    // 从cookie中获取用户ID
    const userIdCookie = request.cookies.get('user_id')
    if (!userIdCookie) {
      return null
    }

    const userId = parseInt(userIdCookie.value)
    
    // 根据ID查找用户
    const user = await userQueries.findById.get(userId)
    
    return user || null
  } catch (error) {
    console.error('获取用户信息失败:', error)
    return null
  }
} 